<?php

namespace Admin\Controller;

use Admin\Logic\UserLogic;

class UserController extends CommonController
{
    /**
     * 管理员列表展示
     * @return [type] [description]
     */
    public function index()
    {
        $param = [];
        if (IS_POST) {
            $param = [
                'start_time' => I('post.start'),
                'end_time' => I('post.end'),
                'keywords' => I('post.keywords')
            ];
        }
        $data = UserLogic::getAll($param);

        $this->view_data['data'] = $data;
        $this->view_data['param'] = $param;
        $this->assign('data', $this->view_data);
        $this->display();
    }

    /**
     * 添加管理员视图
     */
    public function add()
    {
        $this->display();
    }

    /**
     * 处理添加管理员逻辑
     * @return [type] [description]
     */
    public function store()
    {
        $this->checkPost();

        $param = [
            'user_name' => I('post.username', '', 'htmlspecialchars'),
            'pass_word' => I('post.password', '', 'htmlspecialchars'),
            'email' => I('post.email'),
            'is_lock' => I('post.open') == 'on' ? 0 : 1,
            'created_at' => getDateTime()
        ];

        $user = UserLogic::create($param);

        if ($user['code'] == 1) {
            return $this->ajaxError($user['msg']);
        } else {
            return $this->ajaxSuccess();
        }
    }

    /**
     * 编辑用户信息
     * @return [type] [description]
     */
    public function edit()
    {
        $id = I('get.id', 0, 'intval');

        if ($id < 1) {
            E('非法操作');
        }

        $user = D('User')->where(['id' => $id])->find();
        $this->assign('user', $user);
        $this->display();
    }

    /**
     * 处理更新用户信息
     * @return [type] [description]
     */
    public function update()
    {
        $this->checkPost();
        $param = [
            'id' => I('post.id', 0, 'intval'),
            'user_name' => I('post.username'),
            'email' => I('post.email'),
            'is_lock' => I('post.is_lock'),
            'update_at' => date('Y-m-d H:i:s')
        ];

        if (!empty(I('post.password'))) {
            $param['pass_word'] = I('post.password');
        }

        $update = UserLogic::update($param);

        if ($update === false) {
           return $this->ajaxError('服务器找不到啦~^_^');
        }

        return $this->ajaxSuccess();
        
    }

    /**
     * 删除管理员用户
     * @return [type] [description]
     */
    public function delete()
    {
        $id = I('get.id', 0, 'intval');
        $data = UserLogic::delete($id);
        if ($data > 0) {
            return $this->ajaxSuccess();
        } else {
            return $this->ajaxError('删除失败，请重试');
        }
    }

    /**
     * 批量删除管理员用户
     * @return [type] [description]
     */
    public function deleteAll()
    {
        $ids = I('get.ids');
        $data = UserLogic::deleteAll($ids);
        if ($data > 0) {
            return $this->ajaxSuccess();
        } else {
            return $this->ajaxError('删除失败，请重试');
        }
    }
}